package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000117641253";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCaDLnkOcM7vhvs19+1FRxO9fn96lseYHW/cWR+m5vc4/84R9rTlKNV3DBmMXTFXnIHBGliarqYJX6WQ9ujr6LTAPI6Si1+uPRsYMWHJoO2vniTUxU4WaXwFL0nDjNhRUsMWltU4QfYFaRCShgzRo7102STmbQ2hCu116Ec7p6VmZCQcCkpiu3WoZw1N0Wv05jZVVycCpHSO0QcnqBc9fZdto+8zyLW+yljLENwS5//jig/TDLZbnCpx1NUbZJwk2F7ipL+AxoEC9SuQUIt/gnkosjuBuMUsCMoCDyVtsMezZwupCXF1f61pb428WeQwVBIeftoMhHEK9+m/KLK5GBrAgMBAAECggEAHhqUjRP4ogkhneQFtHZepNOOud79VW9WM0YudEtmZwErE81+5s/qJ1IzJxVNE3DEabij+3aN5kOVfDnghxlAgWly7I8oWtY79Z3nm+Sb4q9QEsIuqGdkdQkoeaRdyAlphA14QZsEFGcORWIDXqnQMBvSUMG9pNWpUQzFUEkVNniP82W0d3lOCdDQD4lTkVEe13qSTSfGADzCmeFWHmMro38WJsir3dqZoM1pOXgiEVUS4bQ/Iphs0It/sFHe26/PQF80oQrLF6lEPC9Ag99Fq3FXFw9l/yA2C1tkW6lMm/KWDYCB/aRAEdEhRNuFddlRV/CRCPnVmnfOdNKJrnEpmQKBgQDwAqfoohwrgrNG7xKB6ioy7LFp+cQ6Hr225UoJcwLTJRcAwHeOypskqe8CNzt7TJY9gxgYeMY7dj5t5yKYeLRU2Kap15A+0dW1tveSWCdFpkzQbt7h2oN9y3UpLcCnfvzi8z2NsWZGReQTZc1zFJxaWknYzRrlUHduEls9JL265wKBgQCkUAXccLCNiMj522TtdNol96VpUH98+wQfCyfk21T7yhbXIPxQsdXTCu8Ol83ZT56zkd02uag9VmOxHoE5JH5X7nW2A2EfjLb4+kvkQ5+GpJRn6GOSMo3t6Rvi/dOFe0BczQKJ9u1/WK9lQVfhWRaGU4jOpBF3Fcm7yZTk0ePh3QKBgQCAJerhGy1PTgnK5ftLwmulpRtVKjbLkX5aGTLeuLnZYwRVMm6jnyoW+WTUGgl4W/wb+9LxvgqZ1RigNbkTD9+5LbRPGvm5kPA6hebKDKdrWM/Kumfmz3VFxUxZIEStKfB8fMNw1Dd3bfnJIaIAwBB7bZngWCL0LXKXItX0X5muIQKBgH/BaI05YUzqqbj7iMHINUWIA6ia2kVGtrGcbuxWTQFo49Fo6EWlv+dTfQQPrBHnAjIEm8dNGy512RWB7l+LrGCaHBPlFML/GdUEZ7WXjpWVqX4RBjpJ7Giq8u/PA4OpjSWakPrMb2Swg89quvWP2x+eG6TvlFTPMAc+ePJg75pRAoGBAL36Xu4B0AuuWi42DwDLSY0GbfAxqt+p/MjNKWmpAoccYxi+Fd/Hdg1WeuwEVqfsBTg5jlpAfuSti9UuJ1SwLYBRKNmloJdQFP5NI6xG/P1vUl0N6AhBM8AEwmYd+G63t3wEJ+3K7skOqKXxByyzd8v5Lrwv3aIL92VA4Rx8Xm4k";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqxwT/caxaPvdnc7qN+G2ng7tQBUchvVNIuL7dAd2XVMQokGX7gCiO3kkvLcEcCOHeGaAsFvZpLE0oGd4WtTZkvRflrGcYlLNlEnp8Eplhlpr0f3+bQ7Fs/dah1UwjKsVWe4Yu3j5OlnM2DnLkgpD9xygqrSgpuv2rLWpG/VC2oEruT10tW6bdBWsGQR2PRxj7fDdR0PgW3H3hb77wma0VdeU0gg/+SDfTqgDWY19EUiQ7LBR9aqk1FXkZsXLk+Z61bVZCa0VG/dKcnVzBumvirh3iBKIaRf3eDKcBQDmyFKaYPVz4hm9RkFTtgeQ0THqgUPAm5R4lM9bQh1qvg7ENwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://x8vqgtnqub.52http.tech/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
